package com.alliky.mvprabit.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.alliky.core.config.Config;
import com.alliky.core.pay.PayListenerUtil;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI mIWXAPI;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWeChat();
    }

    private void initWeChat() {
        mIWXAPI = WXAPIFactory.createWXAPI(this, Config.getWechatAppId());
        mIWXAPI.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        mIWXAPI.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK://支付成功
                PayListenerUtil.getInstance().addSuccess();
                break;
            case BaseResp.ErrCode.ERR_COMM://支付失败
                PayListenerUtil.getInstance().addError();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL://用户取消
                PayListenerUtil.getInstance().addCancel();
                break;
        }
        finish();
    }
}
